package com.example.leetdemo.process;

import java.util.Random;

public class Solution382 {

    ListNode head;

    public Solution382(ListNode head) {
        this.head = head;
    }

    public int getRandom() {
        ListNode p = head;
        ListNode q = p;
        int size = 2;
        while (p != null) {
            if ((int) (Math.random() * size) % size > 1) {
                q = q.next;
            }
            p = p.next;
            size++;
        }
        if (q == null) {
            q = head;
        }
        return q.val;
    }

    class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

    public static void main(String[] args) {
        Random random = new Random();
        System.err.println(random.nextInt(100));
        for (int i = 0; i < 100; i++) {
//            System.err.println(test());
        }
    }

    public static int test() {
        int result = 0;
        for (int i = 1; i < 11; i++) {
            if((int) (Math.random() * i) % i == 1) {
                result++;
            }
        }
        return result;
    }
}